home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 8 / Commodore_Free_Issue_08_2007_Commodore_Computer_Club.d64 / t.hexfiles 3 < prev    next >
File List  |  2023-02-26  |  6KB  |  217 lines

  1. uTHE HEX FILES PART 3 written by Jason
  2.  
  3. Welcome again, here's the third
  4. instalment of Hex Files for your
  5. delight, delectation & several other
  6. words beginning with the letter D that
  7. sound quite good. And, before we start
  8. proper, I'll just give you the solution
  9. to the little teaser I posed at the end
  10. of the previous article.
  11.  
  12. Now, one of the joys of machine code is
  13. that there are several ways to reach
  14. the same solution and, whilst some are
  15. better than others, they're all valid
  16. at this stage in the game.
  17.  
  18. So, whilst there are other methods that
  19. could be used (if you found & used it,
  20. it's not the wrong answer!) the easiest
  21. way to modify the routine in the last
  22. instalment would be something like:
  23.  
  24. * = $0900
  25. ldx #$00
  26. lda #$03 ; change the character loop
  27. sta $0400,x
  28. inx
  29. cpx #$0b ; change the number of repeats
  30. bne loop
  31. rts
  32.  
  33. Okay, lets have some fun with a loop.
  34. One of the most common things in demos
  35. (& in fact most games too) is the
  36. scrolling message & what we are going
  37. to do is a simple one with some limits.
  38. As we have seen from the example last
  39. issue it's possible to put characters
  40. on the screen very fast, so fast in
  41. fact that we can't actually see it
  42. happen.
  43.  
  44. It's also possible to move characters
  45. around the screen using loops. Start up
  46. your text editor, tab a couple of times
  47. to get the cursor to its start position
  48. & enter this program:
  49.  
  50. * = $0900
  51. main ldx #$00
  52. move_loop lda $0401,x
  53. sta $0400,x
  54. inx
  55. cpx #$27
  56. bne move_loop
  57. inc $0427
  58. jmp main
  59.  
  60. Before we run it lets look at what you
  61. have typed. The * command is as we used
  62. before, telling C64Asm we want our code
  63. at $0900 (again, 2304 in decimal). We
  64. then have a label called main which is
  65. the start of our main code (hence the
  66. name!) & that clears the X register
  67. again as we have done before. The main
  68. loop of the program (named with the
  69. appropriate label again) is new though.
  70.  
  71. It reads from screen position $0401 &
  72. puts whatever it has read into $0400.
  73. Then the X register goes up one & it
  74. repeats that until X reaches $27 (which
  75. is 39 in decimal). Why stop at 39?
  76. Well, by the time X gets to 40 (by the
  77. time we're checking X it's been
  78. INCremented, remember) the routine is
  79. reading from $0428 (the start of the
  80. second line of the screen) & writing to
  81. $0427 (the right hand end of the first
  82. line) so if we wait we would be reading
  83. the first character of the next line of
  84. the screen! Finally we just play with
  85. the character at the top right of the
  86. screen to make something to look at (by
  87. constantly INCrementing it to make it
  88. show every character the C64 has).
  89.  
  90. Okay, lets crank it up & watch it go!
  91. Assembling is as before, SAVE the file
  92. from the text editor as scroll.asm (the
  93. extension meaning "assembly code", not
  94. even vaguely essential to the process
  95. but it makes remembering what the files
  96. are a lot easier), type c64asm
  97. scroll.asm scroll.prg from DOS whilst
  98. in the correct directory & finally drag
  99. & drop the PRG into WinVICE & SYS2304
  100. to start it.
  101.  
  102. Oh! Now something is happening but
  103. because machine code is so fast we
  104. can't see what, so we need to slow
  105. things down a bit & to do that I'll
  106. introduce a new friend in the form of a
  107. location in the VIC-II chip.
  108.  
  109. Location $D012 (or 53,266 in decimal)
  110. is known as the raster register. The
  111. raster is a line that moves down the
  112. C64's screen redrawing it fifty times a
  113. second & there are over three hundred
  114. "raster lines" on a standard PAL C64
  115. (there are less lines & a faster
  116. refresh speed for NTSC machines) & it's
  117. possible to wait for a specific line &
  118. do something when you get there. Lets
  119. alter our example to take advantage of
  120. this, go back to the source & enter the
  121. following just after the ldx #$00 on
  122. the second line:
  123.  
  124. lda #$fe
  125. raster cmp $d012
  126. bne raster
  127.  
  128. SAVE it back out this time as
  129. scroll2.asm & assemble it as before.
  130. These three new lines set the A
  131. register up with a value of $fe (254 in
  132. decimal), then compare that to whatever
  133. $D012 contains in the same way as we
  134. compared numbers in the previous
  135. installment & if it's not the same (in
  136. other words if the raster isn't at
  137. position $fe) then the Branch if Not
  138. Equal (BNE) back to raster keeps it
  139. waiting in that loop until it is.
  140.  
  141. You should now see loads of characters
  142. scrolling across the top of the screen
  143. very fast but not so fast that you
  144. can't see what's going on. Don't worry
  145. about the odd jump, we are only
  146. experimenting at this point & every now
  147. & then the C64 will miss a beat because
  148. it's busy doing it's housekeeping at
  149. rasterline $fe. Believe it or not this
  150. is moving fifty times a second! Okay,
  151. so one final trick for our new listing
  152. I think, flip back over to the text
  153. editor & alter the routine to read like
  154. this:
  155.  
  156. * = $0900
  157. ldy #$00 ; this is new
  158. main ldx #$00
  159. lda #$fe
  160. raster cmp $d012
  161. bne raster
  162. move_loop lda $0401,x
  163. sta $0400,x
  164. inx
  165. cpx #$27
  166. bne move_loop
  167. lda $a1ff,y ; this line is new too
  168. sta $0427   ; the INC command used to
  169.   be here
  170. iny ; & this line is new as well
  171. jmp main
  172.  
  173. SAVE the source out as scroll3.asm,
  174. assemble & execute again, & if you
  175. press the SHIFT & Commodore keys you'll
  176. see words flying across your screen!
  177. What we are actually doing in these new
  178. bits is using the Y register as a
  179. counter & reading from the C64's memory
  180. at $A1FF onwards for 256 bytes. So
  181. where do the words come from? Well,
  182. $A1FF is actually where the C64 keeps
  183. some of its error messages & this is
  184. what you're seeing.
  185.  
  186. Well, I think that's about enough for
  187. this installment but before I go
  188. another couple of little challenges for
  189. you all to see if you've got the gist;
  190. at the moment the routine reads it's
  191. data from location $A1FF but can you
  192. change it to read from $E460? And can
  193. you make it work on the second line of
  194. the screen rather than the first
  195. (remember to change all of the
  196. references to the screen). I'll give
  197. the answers in the next thrilling
  198. installment but If you have any
  199. questions about this article, machine
  200. code or Flamenco dancing, email me &
  201. I'll see what I can do. Oh, except
  202. about the dancing.
  203.  
  204. The source code for the routines above
  205. can be downloaded:
  206.  
  207. http://www.oldschool-gaming.com/
  208. files/c64/hex_files/
  209. part_3_files.zipfiles/c64/hex_files/
  210. part_3_files.zip
  211.  for easier reference.
  212.  
  213. Printed with Permission from Jason
  214. Taken from the Oldschool gaming website
  215. http://www.oldschool-gaming.com/
  216. c64_hex_files.php
  217.